home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Browsers, Managers & Extensions / Mozilla Weave 0.2.7 / latest-weave.xpi / chrome / sync.jar / content / fx-weave-overlay.js < prev    next >
Text File  |  2008-09-19  |  7KB  |  197 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is Bookmarks sync code.
  15.  *
  16.  * The Initial Developer of the Original Code is Mozilla.
  17.  * Portions created by the Initial Developer are Copyright (C) 2007
  18.  * the Initial Developer. All Rights Reserved.
  19.  *
  20.  * Contributor(s):
  21.  *  Dan Mills <thunder@mozilla.com>
  22.  *  Chris Beard <cbeard@mozilla.com>
  23.  *  Dan Mosedale <dmose@mozilla.org>
  24.  *
  25.  * Alternatively, the contents of this file may be used under the terms of
  26.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  27.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  28.  * in which case the provisions of the GPL or the LGPL are applicable instead
  29.  * of those above. If you wish to allow use of your version of this file only
  30.  * under the terms of either the GPL or the LGPL, and not to allow others to
  31.  * use your version of this file under the terms of the MPL, indicate your
  32.  * decision by deleting the provisions above and replace them with the notice
  33.  * and other provisions required by the GPL or the LGPL. If you do not delete
  34.  * the provisions above, a recipient may use your version of this file under
  35.  * the terms of any one of the MPL, the GPL or the LGPL.
  36.  *
  37.  * ***** END LICENSE BLOCK ***** */
  38.  
  39. function FxWeaveGlue() {
  40.   this._log = Log4Moz.Service.getLogger("Chrome.Window");
  41.  
  42.   this._log.info("Initializing Firefox Weave embedding");
  43.  
  44.   this._os.addObserver(this, "weave:service:tabs-engine:sync:success", false);
  45.   var engines = [
  46.     new Weave.BookmarksEngine(),
  47.     new Weave.HistoryEngine(),
  48.     new Weave.CookieEngine(),
  49.     new Weave.PasswordEngine(),
  50.     new Weave.FormEngine(),
  51.     new Weave.TabEngine(),
  52.     new Weave.InputEngine()
  53.   ];
  54.  
  55.   // Register engines
  56.   for (let i = 0; i < engines.length; i++)
  57.     Weave.Engines.register(engines[i]);
  58.  
  59.   // Display a tabs notification if there are any virtual tabs.
  60.   // FIXME this won't do anything, because virtualTabs get loaded
  61.   this._onVirtualTabsChanged();
  62.  
  63.   return;
  64. }
  65. FxWeaveGlue.prototype = {
  66.   _log: null,
  67.  
  68.   __os: null,
  69.   get _os() {
  70.     if (!this.__os)
  71.       this.__os = Cc["@mozilla.org/observer-service;1"]
  72.         .getService(Ci.nsIObserverService);
  73.     return this.__os;
  74.   },
  75.  
  76.   get _sessionStore() {
  77.       let sessionStore = Cc["@mozilla.org/browser/sessionstore;1"].
  78.                  getService(Ci.nsISessionStore);
  79.       this.__defineGetter__("_sessionStore", function() sessionStore);
  80.       return this._sessionStore;
  81.   },
  82.  
  83.   get _json() {
  84.       let json = Cc["@mozilla.org/dom/json;1"].createInstance(Ci.nsIJSON);
  85.       this.__defineGetter__("_json", function() json);
  86.       return this._json;
  87.   },
  88.  
  89.   // FIXME: refactor this function with the identical one in notification.xml.
  90.   _getVirtualTabs: function FxWeaveGlue__getVirtualTabs() {
  91.     let virtualTabs = Weave.Engines.get("tabs").virtualTabs;
  92.  
  93.     // Convert the hash of virtual tabs indexed by ID into an array
  94.     // of virtual tabs whose ID is stored in an ID property.
  95.     virtualTabs = [tab for each (tab in virtualTabs)];
  96.     for (let i = 0; i < virtualTabs.length; i++)
  97.       virtualTabs[i].id = i;
  98.  
  99.     // Sort virtual tabs by their position in their windows.
  100.     // Note: we don't actually group by window first, so all first tabs
  101.     // will appear first in the list, followed by all second tabs, and so on.
  102.     // FIXME: group by window, even though we aren't opening them up that way,
  103.     // so the list better resembles the pattern the user remembers.
  104.     virtualTabs.sort(function(a, b) a.position > b.position ?  1 :
  105.                                     a.position < b.position ? -1 : 0);
  106.  
  107.     return virtualTabs;
  108.   },
  109.  
  110.   doInitTabsMenu: function FxWeaveGlue__doInitTabsMenu() {
  111.     let menu = document.getElementById("sync-tabs-menu");
  112.     let virtualTabs = this._getVirtualTabs();
  113.  
  114.     while (menu.itemCount > 1)
  115.       menu.removeItemAt(menu.itemCount - 1);
  116.  
  117.     for each (let virtualTab in virtualTabs) {
  118.       let currentEntry = virtualTab.state.entries[virtualTab.state.index - 1];
  119.       if (!currentEntry)
  120.         continue;
  121.       let label = currentEntry.title ? currentEntry.title : currentEntry.url;
  122.       let menuitem = menu.appendItem(label, virtualTab.id);
  123.       menuitem.label = label;
  124.       menuitem.value = virtualTab.id;
  125.  
  126.       // Make a tooltip that contains either or both of the title and URL.
  127.       menuitem.tooltipText =
  128.         [currentEntry.title, currentEntry.url].filter(function(v) v).join("\n");
  129.     }
  130.  
  131.     document.getElementById("sync-no-tabs-menu-item").hidden =
  132.       (menu.itemCount > 1);
  133.   },
  134.  
  135.   onCommandTabsMenu: function FxWeaveGlue_onCommandTabsMenu(event) {
  136.     let tabID = event.target.value;
  137.     let virtualTabs = this._getVirtualTabs();
  138.     let virtualTab = virtualTabs[tabID];
  139.     let tab = gBrowser.addTab("about:blank");
  140.     this._sessionStore.setTabState(tab, this._json.encode(virtualTab.state));
  141.     gBrowser.selectedTab = tab;
  142.     delete virtualTabs[tabID];
  143.  
  144.     // FIXME: update a notification that lists the opened tab, if any.
  145.   },
  146.  
  147.   _onVirtualTabsChanged: function Sync__onVirtualTabsChanged() {
  148.     let virtualTabs = this._getVirtualTabs();
  149.  
  150.     // Get the (first, which should also be the only) notification, if any.
  151.     let [existingNotification] =
  152.       Weave.Notifications.notifications.
  153.       filter(function(v) v.constructor.name == "TabsNotification");
  154.  
  155.     // As long as there is at least one virtual tab that hasn't previously been
  156.     // disposed of by the user, notify the user about available tabs.
  157.     for each (let virtualTab in virtualTabs) {
  158.       if (!virtualTab._disposed) {
  159.         // If there's an existing tabs notification, update it.
  160.         // FIXME: make tabs notifications automatically update themselves.
  161.         let newNotification = new Weave.TabsNotification();
  162.         if (existingNotification)
  163.           Weave.Notifications.replace(existingNotification, newNotification);
  164.         else
  165.           Weave.Notifications.add(newNotification);
  166.  
  167.         return;
  168.       }
  169.     }
  170.  
  171.     // If we've gotten this far, it means there are no tabs left to notify
  172.     // the user about, so remove the existing notification, if any.
  173.     if (existingNotification)
  174.       Weave.Notifications.remove(existingNotification);
  175.   },
  176.  
  177.   // nsIObserver
  178.   observe: function FxWeaveGlue__observe(subject, topic, data) {
  179.     switch(topic) {
  180.     case "weave:service:tabs-engine:sync:success":
  181.       this._onVirtualTabsChanged();
  182.       break;
  183.     default:
  184.       break;
  185.     }
  186.   },
  187.  
  188.   shutdown: function FxWeaveGlue__shutdown() {
  189.     this._os.removeObserver(this, "weave:service:tabs-engine:sync:success");
  190.   }
  191. }
  192.  
  193. let gFxWeaveGlue;
  194.  
  195. window.addEventListener("load", function(e) { gFxWeaveGlue = new FxWeaveGlue(); }, false);
  196. window.addEventListener("unload", function (e) { gFxWeaveGlue.shutdown(e); }, false);
  197.